package com.gwtapps.messenger.client.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowCloseListener;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtapps.messenger.client.model.Contact;
import com.gwtapps.messenger.client.model.ContactList;

public class MessengerView extends Composite implements WindowCloseListener, WindowResizeListener, HistoryListener{
	
	private DeckPanel mainPanel = new DeckPanel();
	private ContactListView contactListView;
	private MapView mapView;
	private SignInView signIn;
	public ContactList contactList;
	private List<ChatWindowView> openChatWindows = new ArrayList<ChatWindowView>();
	private Map<String, ChatWindowView> allChatWindows = new HashMap<String, ChatWindowView>();
	private MessengerViewListener listener;
	
	public MessengerView( MessengerViewListener listener ){
		initWidget( mainPanel );
		this.listener = listener;
		signIn = new SignInView( this );
		
		mainPanel.add( signIn );
		mainPanel.showWidget(0);
		
		Window.addWindowCloseListener(this);
		Window.addWindowResizeListener(this);
		History.addHistoryListener(this);
	}
	
	public MessengerViewListener getListener(){
		return listener;
	}
	
	public ContactListView getContactListView(){
		return contactListView;
	}
	public MapView getMapView(){
		return mapView;
	}
	
	
	public ContactList getContactList(){
		return contactList;
	}
	
	public void setContactList( ContactList contactList ){
		this.contactList = contactList;
		if( contactListView == null ){
			contactListView = new ContactListView( this );
			mainPanel.add( contactListView );
		}
		mainPanel.showWidget(1);
	}
	
	public void setMap(ContactList contactList ){
		this.contactList = contactList;
		if( mapView == null ){
			mapView = new MapView( this );
			RootPanel.get("gameArea").add(mapView);
		}
	}
	
	
	public void onWindowClosed(){}
	
	public String onWindowClosing(){
		listener.onSignOut();
		return null;
	}
	
	public void onWindowResized( int width, int height ){
		resetChatWindowPositions();
	}
	
	public ChatWindowView getChatWindowView( Contact contact ){
		
		ChatWindowView chatWindow = (ChatWindowView)allChatWindows.get( contact.getName() );
		if( chatWindow == null ){
			chatWindow = new ChatWindowView( this, contact );
			allChatWindows.put( contact.getName(), chatWindow );
		}
		if( chatWindow.position == -1 ){
			chatWindow.position = openChatWindows.size();
			openChatWindows.add( chatWindow );
		}
		chatWindow.show();	
		return chatWindow;		
	}

	public void closeChatWindow( ChatWindowView windowToClose ){
		openChatWindows.remove( windowToClose.position );		
		windowToClose.setVisible(false);
		windowToClose.position = -1;
		resetChatWindowPositions();
	}
	
	protected void resetChatWindowPositions(){
		for( int i=0; i< openChatWindows.size(); ++i ){
			ChatWindowView chatWindow = (ChatWindowView)openChatWindows.get(i);
			chatWindow.setPosition(i);
		}
	}
	
	public void onHistoryChanged( String historyToken ) {
		Contact contact = contactList.getContact( historyToken );
		if( contact != null ){
			getChatWindowView( contact );
		}
	}
}
